package com.gimi.cloud.jimi.queue.revicer;

import com.gimi.cloud.jimi.common.mq.enume.QueueConstant;
import com.gimi.cloud.jimi.dao.dao.sys.SysMqLogDao;
import com.gimi.cloud.jimi.dto.app.moreInfo.CreditcardLoginDTO;
import com.gimi.cloud.jimi.entity.sys.SysMqLogEntity;
import com.gimi.cloud.jimi.queue.service.FengchaoCreditcardWaterService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @auther loongs
 * @date 2018/1/16  14:55
 */
@Component
public class FengchaoCreditcardWaterReceived {
    private static final Logger logger = LoggerFactory.getLogger(FengchaoCreditcardWaterReceived.class);
    private FengchaoCreditcardWaterService fengchaoCreditcardWaterService;
    SysMqLogDao sysMqLogDao;

    @Autowired
    public FengchaoCreditcardWaterReceived(FengchaoCreditcardWaterService fengchaoCreditcardWaterService,SysMqLogDao sysMqLogDao) {
        this.fengchaoCreditcardWaterService = fengchaoCreditcardWaterService;
        this.sysMqLogDao = sysMqLogDao;
    }

    /**
     * 获取蜂巢信用卡流水队列
     *
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = {QueueConstant.FCXYKLS_QUEUE_NAME})
    public void reveiveOperationLog(Message message, Channel channel, CreditcardLoginDTO creditcardLoginDTO) throws Exception {
        logger.info("消息队列，获取蜂巢信用卡流水队列开始,入参："+creditcardLoginDTO);
        SysMqLogEntity mqLog = new SysMqLogEntity();
        mqLog.setName("获取蜂巢信用卡流水队列");
        mqLog.setStartTime(new Date());
        mqLog.setUserId(creditcardLoginDTO.getUserId());
        mqLog.setParam(creditcardLoginDTO.toString());
        try {
            fengchaoCreditcardWaterService.reveiveCreditcardWaterData(message, channel, creditcardLoginDTO);
        }catch(Exception e){
            logger.info("获取蜂巢信用卡流水失败，已自动取消!");
            e.printStackTrace();
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
        mqLog.setEndTime(new Date());
        sysMqLogDao.insertSelective(mqLog);
        logger.info("消息队列，获取蜂巢信用卡流水队列结束");
    }
}
